package com.niuxiaofa.newlaw.Activity;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.PopupWindow;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.niuxiaofa.newlaw.Base.BaseActivity;
import com.niuxiaofa.newlaw.Bean.AdvisoryTagBean;
import com.niuxiaofa.newlaw.Network.WisdomLibraryPresenter;
import com.niuxiaofa.newlaw.R;
import com.niuxiaofa.newlaw.Tools.RecyclerViewHelp;
import com.niuxiaofa.newlaw.adapter.ConsultationAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;


/**
 *
 * 咨询
 *
 */
public class ConsultationActivity extends BaseActivity implements WisdomLibraryPresenter.interface_advisoryTag {


    @BindView(R.id.pic_consulting)
    View pic_consulting;

    @BindView(R.id.sound_consulting)
    View sound_consulting;


    ConsultationAdapter consultationAdapter1, consultationAdapter2;

    PopupWindow popupWindow;

    @BindView(R.id.showConsultationKind)
    TextView showConsultationKind;

    List<AdvisoryTagBean.DataDTO> listFather = new ArrayList<>();

    List<AdvisoryTagBean.DataDTO> listChild = new ArrayList<>();

    boolean advisoryTagParent = true;

    int tag1 = 0;
    int tag2 = 0;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_advisory;
    }

    @Override
    protected void initData() {
        setTitle("咨询");


        WisdomLibraryPresenter.advisoryTag("", this);

        pic_consulting.setOnClickListener(e -> {
            pic_consulting.setBackgroundResource(R.drawable.red_shap);
            sound_consulting.setBackgroundResource(R.drawable.shap_gray_semicircle);
        });
        sound_consulting.setOnClickListener(e -> {
            sound_consulting.setBackgroundResource(R.drawable.red_shap);
            pic_consulting.setBackgroundResource(R.drawable.shap_gray_semicircle);
        });

        showConsultationKind.setOnClickListener(e -> {
            showConsultationKind();
        });


        consultationAdapter1 = new ConsultationAdapter(listFather, new ConsultationAdapter.Onclick() {
            @Override
            public void onclick(int c) {
                WisdomLibraryPresenter.advisoryTag(listFather.get(c).getId(), ConsultationActivity.this);
                tag1=c;

            }
        });

        consultationAdapter2 = new ConsultationAdapter(listChild, new ConsultationAdapter.Onclick() {
            @Override
            public void onclick(int c) {
                tag2 =c;
            }
        });


    }


    private void showConsultationKind() {

        View contentView = View.inflate(this, R.layout.pop_consultation_kind, null);

        popupWindow = new PopupWindow(contentView,
                WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT, true);
        //初始化视图
        RecyclerView recyclerView = contentView.findViewById(R.id.advisoryTag);
        RecyclerView recyclerView2 = contentView.findViewById(R.id.advisoryTag2);
        RecyclerViewHelp.setRecyclerGridLayout(recyclerView, 5, 4).setAdapter(consultationAdapter1);
        RecyclerViewHelp.setRecyclerGridLayout(recyclerView2, 5, 4).setAdapter(consultationAdapter2);

        TextView complete = contentView.findViewById(R.id.complete);
        ;
        complete.setOnClickListener(e -> {

            showConsultationKind.setText(listFather.get(tag1).getName()+","+ (listChild.size()==0? "": listChild.get(tag2).getName()));
            popupWindow.dismiss();

        });

        backgroundAlpha(0.5f);
        popupWindow.setFocusable(true);
        popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        popupWindow.setOutsideTouchable(true);
        popupWindow.setAnimationStyle(R.style.anim_menu_bottombar);

        //消失监bai听听
        popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
            @Override
            public void onDismiss() {
                backgroundAlpha(1f);
            }
        });

        // showSoft(commentEdittext);
        popupWindow.showAtLocation(this.getWindow().getDecorView(), Gravity.BOTTOM, 0, 0);


    }

    /**
     * 设置添加屏幕的背景透明度
     *
     * @param bgAlpha
     */
    public void backgroundAlpha(float bgAlpha) {
        this.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        WindowManager.LayoutParams lp = this.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        this.getWindow().setAttributes(lp);
    }

    @Override
    public void getResponse(AdvisoryTagBean u) {
        if (u.getCode() == 1) {
            if (advisoryTagParent) {
                listFather.addAll(u.getData());
                advisoryTagParent = false;
                consultationAdapter1.notifyDataSetChanged();

            } else {
                listChild.clear();
                listChild.addAll(u.getData());
                consultationAdapter2.notifyDataSetChanged();
            }

        }
    }
}
